const App = getApp();
import Notify from '../../../dist/notify/notify';
import Toast from '../../../dist/toast/toast';

// 获取某月天数
const getdays = (month) => {
  // debugger
  let year = new Date().getFullYear();
  var d = new Date(year, month, 0);
  // console.log(d.getDate())
  return d.getDate();
}

const monthMethod = () => {
  let json = {}
  let arr = []
  for (let i = 1; i < 13; i++) {
    if (i < 10) {
      arr.push(`0${i}月`)
    } else {
      arr.push(`${i}月`)
    }
  }
  json.values = arr;
  json.defaultIndex = new Date().getMonth()
  json.className = "column1"
  return json;
}


// 小时数据
const hoursMethod = () => {
  let json = {}
  let arr = []
  for (let i = 1; i < 25; i++) {
    if (i < 10) {
      arr.push(`0${i}`)
    } else {
      arr.push(`${i}`)
    }
  }
  json.values = arr;
  // json.defaultIndex = new Date().getHours()-1
  json.defaultIndex = 8
  return json;
}

// 天数数据
const daysMethod = (num) => {
  let json = {}
  let arr = []
  for (let i = 1; i < num + 1; i++) {
    if (i < 10) {
      arr.push(`0${i}日`)
    } else {
      arr.push(`${i}日`)
    }
  }
  json.values = arr;
  json.defaultIndex = new Date().getDate() - 1
  json.className = "column2"
  return json;
}

// 初始化时间数据
const myMonth = monthMethod()
const myHours = hoursMethod()
let nowMonth = new Date().getMonth() + 1;
const myDays = daysMethod(getdays(nowMonth))


Page({
  data: {
    data: [{
      id: 1
    }, {
      id: 2
    }, {
      id: 3
    }, {
      id: 4
    }],
    checked: [],
    activeIcon: '//img.yzcdn.cn/icon-active.png',
    inactiveIcon: '//img.yzcdn.cn/icon-normal.png',
    imgUrl: App.globalData.imgUrl,
    isPageType: 1,
    isLoading: false,
    reLoading: false,
    features_id: 0,
    isFullSucreen: App.globalData.isFullSucreen, //判断机型
    hara_url: '',
    ad_img: '',
    ad_img_id: 0,
    file_id: 0,
    tmp_img: '',
    wechatUrl: '',
    aliUrl: '',
    share_title: '',
    ad_img_list: [],
    inputLists: [{}],
    videoSrc: '',
    videoType: '',
    replace: {
      file: '',
      text: '',
    },
    videoAd: null,
    isArea: false,
    isCatetory: false,
    switch1Checked: false,
    switch2Checked: false,
    radioChecked: 'r1',
    isTipsVideoShow: false,
    columns: ['杭州', '宁波', '温州', '嘉兴', '湖州'],
    columnsTime: [
      myMonth, myDays, myHours, myHours
    ],
    adTimeList: [],
    showAd: false,
    showAdList: false,
    actionsAd: [],
    inputList: [{
        value: 'value1',
        index: 0
      },
      {
        value: 'value2',
        index: 1
      },
      {
        value: 'value3',
        index: 2
      }
    ],
    // actionsAd: [
    //   {
    //     user_id: '1',
    //     name: '1'
    //   },
    //   {
    //     user_id: '2',
    //     name: '2'
    //   },
    //   {
    //     user_id: '3',
    //     name: '3'
    //   },
    //   {
    //     user_id: '4',
    //     name: '4'
    //   },
    //   {
    //     user_id: '5',
    //     name: '5'
    //   },
    //   {
    //     user_id: '6',
    //     name: '6'
    //   },
    //   {
    //     user_id: '7',
    //     name: '7'
    //   }
    // ],
    actionsAdList: [],
    ggz: {
      id: "",
      name: "",
      adName: ""
    },
    areaData: {},
    catetoryData: {},
    isAddTime: false,
    areaColumns: [],
    // areaColumns: [
    //   "张三", "张一三", "张三丰", "李四", "哈哈哈"
    // ], //区域展示数据
    catetoryColumns1: ['抖音', '快手', '淘宝', '火山', '小红书', '咸鱼', '微视', '飞猪'], //区域展示数据
    catetoryColumns: [{
      checked: false,
      name: '抖音',
      type: 1
    }, {
      checked: false,
      name: '快手',
      type: 2
    }, {
      checked: false,
      name: '淘宝',
      type: 3
    }, {
      checked: false,
      name: '火山',
      type: 4
    }, {
      checked: false,
      name: '小红书',
      type: 5
    }, {
      checked: false,
      name: '咸鱼',
      type: 6
    }, {
      checked: false,
      name: '微视',
      type: 7
    }, {
      checked: false,
      name: '飞猪',
      type: 8
    }], //区域展示数据
    wxAreaList: [], //区域数据
    batchId: [],
    selectCheck: [],
    result: [],
    commandType: '',
    koulingList: [], //口令列表
    koulingInputIndex: 0, //口令下标

    timeDateList: ['天', '周', '月'],
    chooseIndex: 999,
    timeList: ["01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00", "24:00"],
    koulingList1: [],
    weekList: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
    dateList: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'],
    activeStart: 999,
    activeEnd: 999,
    activeKouling: 999,
    addTimeShow: false,
    addWeekShow: false,
    addDateShow: false,
    activeWeek: 999,
    activeKouling1: 999,
    activeDate: 999,
    activeKouling2: 999,
    startTime: '',
    endTime: '',
    koulingDay: '',
    selectTimeList: [], //新投放时间
    weekValue: '',
    DateValue: '',
    koulingWeek: '',
    liuliangList: [],
    liuliangList1: [],
    koulingDayList: [],
    koulingWeekList: [],
    koulingDateList: [],
    lead_path: '', //广告引导页
    tmp_img: '',
    file_id: '',
    ad_img: '',
    adValue: ''
  },
  searchAd() {
    
    var page = 1;
    let _this = this;
    
    App._get('Area/WxAreaList', {
      page,
      userId: wx.getStorageSync('user_id')
    }, res => {
      let list = [];
      res.data.map(item => {
        list.push(item.f_area_name)
      })

      _this.setData({
        wxAreaList: res.data,
        areaColumns: list
        // areaColumns: [
        //       "张三", "张一三", "张三丰", "李四", "哈哈哈"
        //     ]
      })
      if (this.data.adValue != '') {
       // 使用filter和includvares进行模糊查询  
       var searchTerm = this.data.adValue; // 要搜索的字符串  
       var filteredList = this.data.areaColumns.filter(item => item.includes(searchTerm));
 
       // 输出结果  
      
       this.setData({
         areaColumns: filteredList
       })
      }

    }, false, () => {

    })
    // if (this.data.adValue != '') {
      // // 使用filter和includvares进行模糊查询  
      // var searchTerm = this.data.adValue; // 要搜索的字符串  
      // var filteredList = this.data.areaColumns.filter(item => item.includes(searchTerm));

      // // 输出结果  
      // console.log(filteredList, 'filteredList');
      // this.setData({
      //   areaColumns: filteredList
      // })
    // }
  },
  changeAdValue(e) {
    
    this.setData({
      adValue: e.detail.value
    })
  },
  //获取广告引导页默认图片
  getAdLeadTo() {
    let _this = this;
    App._get('Area/AdLeadTo', {

    }, res => {
     
      if (res.code == 1) {
        // _this.setData({
        //   imageAd: res.data.image
        // })
        _this.setData({
          lead_path: res.data.image,
          tmp_img: res.data.image,
          ad_img: res.data.image
        })
      }

    }, false, () => {

    })
  },
  // 引导页图片上传
  chooseImage() {
    let _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        
        _this.setData({
          lead_path: res.tempFilePaths[0],
          tmp_img: res.tempFilePaths[0],
        })
        _this.uploadFile();
      }
    });
  },
  // 执行图片上传
  uploadFile() {
    let _this = this;
    let params = {
      wxapp_id: App.getWxappId(),
      token: wx.getStorageSync('token')
    };
    _this.setData({
      reLoading: true,
    })
    wx.uploadFile({
      url: App.api_root + 'upload/image',
      filePath: _this.data.tmp_img,
      name: 'iFile',
      formData: params,
      success: function (res) {
        let result = typeof res.data === "object" ? res.data : JSON.parse(res.data);
        _this.setData({
          file_id: result.data.file_id,
          ad_img: result.data.file_path,
          reLoading: false,
        })
      }
    });
  },
  toList() {
    // console.log('this.data.isPageType',this.data.isPageType)
    wx.navigateTo({
      url: '/pages/liuliangchiList/liuliangchiList?type=0' + '&isPageType=' + this.data.isPageType,
    })
  },
  // 选中开始时间
  clickStartTime(e) {
    // console.log(e.currentTarget.dataset.index, 'clickStartTime')
    let index = e.currentTarget.dataset.index
    this.setData({
      activeStart: index,
      startTime: this.data.timeList[index]
    })
    // console.log(this.data.startTime)
  },
  // 选中结束时间
  clickEndTime(e) {
    // console.log(e.currentTarget.dataset.index, 'clickEndTime')
    let index = e.currentTarget.dataset.index
    this.setData({
      activeEnd: index,
      endTime: this.data.timeList[index]
    })
  },
  // 选择时间--选中口令
  clickKouling(e) {

    let index = e.currentTarget.dataset.index
    
    this.setData({
      activeKouling: e.currentTarget.dataset.index,
      koulingDay: this.data.liuliangList1[index].title,
      koulingDayList: this.data.liuliangList1[index].koulingList
    })
  },
  // 选择天--确认
  confirmDay() {

    if (!this.data.startTime || !this.data.endTime || !this.data.koulingDay) {
      wx.showToast({
        title: '请选择记录',
        icon: 'none'
      })
      return
    }
    // console.log(this.data.startTime, this.data.endTime, this.data.koulingDay)
    const startMinutes = this.timeStringToMinutes(this.data.startTime);
    const endMinutes = this.timeStringToMinutes(this.data.endTime);
    // console.log(startMinutes, endMinutes); // 输出: 60
    // let objToCompare = {
    //   startTime: this.data.startTime,
    //   endTime: this.data.endTime,
    //   kouling: this.data.koulingDay
    // }
    if (startMinutes >= endMinutes) {
      wx.showToast({
        title: '时间规则错误',
        icon: 'none'
      })
    } else if (this.data.selectTimeList.some(item => item.startTime == this.data.startTime) && this.data.selectTimeList.some(item => item.endTime == this.data.endTime)) {
      wx.showToast({
        title: '时间规则重复',
        icon: 'none'
      })
    } else {
      this.data.selectTimeList.push({
        startTime: this.data.startTime,
        endTime: this.data.endTime,
        weekValue: '',
        DateValue: '',
        kouling: this.data.koulingDay,
        type: 'day',
        koulingDayList: this.data.koulingDayList,
        koulingWeekList: [],
        koulingDateList: []

      })
      this.setData({
        selectTimeList: this.data.selectTimeList
      })
    }
    this.setData({
      addTimeShow: false,
      startTime: '',
      endTime: '',
      activeStart: 999,
      activeEnd: 999,
      activeKouling: 999,
    })
  },
  // 选择天--取消
  cancelDay() {
    this.setData({
      addTimeShow: false,
      startTime: '',
      endTime: '',
      activeStart: 999,
      activeEnd: 999,
      activeKouling: 999,
    })
  },
  // 选中周
  clickWeek(e) {

    let index = e.currentTarget.dataset.index
    
    this.setData({
      activeWeek: index,
      weekValue: this.data.weekList[index]
    })

  },
  // 选中周--口令
  clickKouling1(e) {
    // console.log(e.currentTarget.dataset.index, 'clickKouling')
    let index = e.currentTarget.dataset.index
    this.setData({
      activeKouling1: e.currentTarget.dataset.index,
      koulingWeek: this.data.liuliangList1[index].title,
      koulingWeekList: this.data.liuliangList1[index].koulingList
    })
  },
  clickDate(e) {
    // console.log(e.currentTarget.dataset.index, 'clickDate')
    let index = e.currentTarget.dataset.index
    this.setData({
      activeDate: index,
      DateValue: this.data.dateList[index]
    })
  },
  clickKouling2(e) {
    // console.log(e.currentTarget.dataset.index, 'clickKouling2')
    let index = e.currentTarget.dataset.index
    this.setData({
      activeKouling2: e.currentTarget.dataset.index,
      koulingDate: this.data.liuliangList1[index].title,
      koulingDateList: this.data.liuliangList1[index].koulingList
    })
  },
  // 选择周--确认
  confirmWeek() {
    if (!this.data.weekValue || !this.data.koulingWeek) {
      wx.showToast({
        title: '请选择记录',
        icon: 'none'
      })
      return
    }
    if (this.data.selectTimeList.some(item => item.weekValue == this.data.weekValue)) {
      wx.showToast({
        title: '记录重复',
        icon: 'none'
      })
      return
    }

    this.data.selectTimeList.push({
      startTime: '',
      endTime: '',
      weekValue: this.data.weekValue,
      DateValue: '',
      kouling: this.data.koulingWeek,
      type: 'week',
      koulingDayList: [],
      koulingWeekList: this.data.koulingWeekList,
      koulingDateList: []
    })
    this.setData({
      selectTimeList: this.data.selectTimeList,
      addWeekShow: false,
      weekValue: '',
      activeWeek: 999,
      activeKouling1: 999
    })

  },
  cancelWeek() {
    this.setData({
      addWeekShow: false,
      weekValue: '',
      activeWeek: 999,
      activeKouling1: 999
    })
  },
  confirmDate() {
    // console.log(this.data.DateValue,this.data.koulingDate)
    if (!this.data.DateValue || !this.data.koulingDate) {
      wx.showToast({
        title: '请选择记录',
        icon: 'none'
      })
      return
    }
    if (this.data.selectTimeList.some(item => item.DateValue == this.data.DateValue)) {
      wx.showToast({
        title: '记录重复',
        icon: 'none'
      })
      return
    }

    this.data.selectTimeList.push({
      startTime: '',
      endTime: '',
      weekValue: '',
      DateValue: this.data.DateValue,
      kouling: this.data.koulingDate,
      type: 'date',
      koulingDayList: [],
      koulingWeekList: [],
      koulingDateList: this.data.koulingDateList
    })
    this.setData({
      selectTimeList: this.data.selectTimeList,
      addDateShow: false,
      DateValue: '',
      activeDate: 999,
      activeKouling2: 999
    })
  },
  cancelDate() {
    this.setData({
      addDateShow: false,
      DateValue: '',
      activeDate: 999,
      activeKouling2: 999
    })
  },
  // 删除记录
  delRecord(e) {
    // console.log(e.currentTarget.dataset.index, 'clickKouling2')
    let index = e.currentTarget.dataset.index
    this.data.selectTimeList.splice(index, 1)
    this.setData({
      selectTimeList: this.data.selectTimeList
    })
  },


  cancelAd() {
    this.setData({
      showAd: false
    });
  },
  changeTimeIndex(e) {
    // console.log(e.currentTarget.dataset.index, 'changeTimeIndex')
    this.setData({
      chooseIndex: e.currentTarget.dataset.index,
      selectTimeList: [],
      koulingDayList: [],
      koulingWeekList: [],
      koulingDateList: []
    })
  },
  timeStringToMinutes(timeStr) {
    const [hours, minutes] = timeStr.split(':').map(Number);
    return hours * 60 + minutes;
  },



  // 选择广告主change事件
  handleCheck(event) {
    // console.log(event, 'eeeeeeeeeeeeee')
    // this.setData({
    //   showAd:false,
    //   'ggz.id': event.detail.user_id,
    //   'ggz.name': event.detail.name
    // })
    this.setData({
      result: event.detail,
    });
    // console.log(this.data.result, )
    this.setData({
      'selectCheck': event.detail.value
    })
  },
  // handleCheckBox(e){
  //   console.log(e,'handleCheckBox')
  // },
  confirmAdver() {
    // console.log(this.data.actionsAd, 'confirmAdver')
    let checkedUserId = []
    let checkedName = []
    this.data.actionsAd.forEach(item => {
      if (item.checked == true) {
        checkedUserId.push(item.user_id)
        checkedName.push(item.name)
      }

    })
    // console.log(checkedUserId, checkedName, '')
    this.setData({
      showAd: false,
      'ggz.id': checkedUserId.join(","),
      'ggz.name': checkedName.join(",")
    })
    this.getAdverList(checkedUserId.join(","));
    // this.getAdverList(this.data.selectCheck.join(","));
  },
  ceshi() {
    // console.log(this.data.liuliangList1, 'liuliangList1')
  },
  confirmCatetory1() {
    // console.log(this.data.actionsAd, 'confirmAdver')
    let checkedUserId = []
    // let checkedName =  this.data.koulingList
    let checkedName = []
    let arr = []

    this.data.catetoryColumns.forEach(item => {
      if (item.checked == true) {
        // console.log(item.name, 'llll')

        arr.push({
          name: item.name,
          val: '',
          type: item.type
        })

        // if (!checkedName.includes(e.detail.value)) {
        //   checkedName.push(e.detail.value)
        // }
      }
    })
    checkedName.forEach(items => {
      // console.log(items.name, 'namamamam')
    })
    // console.log(checkedName, '', this.data.koulingList,arr)
    arr.forEach(item => {
      if (!this.data.koulingList.includes(item.name)) {
        checkedName.push({
          name: item.name,
          val: '',
          type: item.type
        })
        // checkedName.push(e.detail.value)
      }
    })
    this.setData({
      isCatetory: false,
      koulingList: checkedName
    })
    // arr.forEach(item => {
    //   if (!this.data.liuliangList1.includes(item.name)) {
    //     checkedName.push({
    //       name: item.name,
    //       val: '',
    //       type: item.type
    //     })
    //     // checkedName.push(e.detail.value)
    //   }
    // })
    // this.setData({
    //   isCatetory: false,
    //   liuliangList1: checkedName
    // })
    // // this.getAdverList(checkedUserId.join(","));
    // // this.getAdverList(this.data.selectCheck.join(","));
  },

  inputChange(e) {
    // console.log('e', e, this.data.koulingInputIndex)
    const index = e.currentTarget.dataset.index;
    let inputList = this.data.koulingList;
    inputList[index].val = e.detail.value;
    this.setData({
      koulingList: inputList
      // koulingType: 
    });
  },
  inputChange1(e) {
    // console.log('inputChange1', e)
    let index = e.currentTarget.dataset.index
    this.setData({
      koulingInputIndex: index
    })
  },

  onSelectAd(event) {
    // debugger
    // console.log(event.detail);
    this.setData({
      showAd: false,
      'ggz.id': event.detail.user_id,
      'ggz.name': event.detail.name
    })
    this.getAdverList(event.detail.user_id);
  },
  // onSelectAdList(event) {
  //   // console.log(event)
  //   this.setData({
  //     'ggz.id': event.detail.id,
  //     'ggz.name': event.detail.name,
  //     showAdList: false
  //   });
  // },
  clickAd() {
    this.setData({
      showAd: true
    })
  },
  confirmArea(e) {
    this.setData({
      isArea: false,
      "areaData.name": e.detail.value,
      "areaData.id": this.data.wxAreaList[e.detail.index].id
    })
    // console.log(this.data.areaData)
  },
  cancelArea() {
    this.setData({
      isArea: false
    })
  },
  clickArea() {
    let temp = !this.data.isArea
    this.setData({
      isArea: temp
    })
  },
  confirmCatetory(e) {
    // console.log(e, 'eeeeeee')
    // let arr = []
    // arr = this.data.koulingList
    // if (!arr.includes(e.detail.value)) {
    //   arr.push(e.detail.value)
    // }

    // console.log(arr, 'rrrrrr')
    // this.setData({
    //   isCatetory: false,
    //   koulingList: arr,
    //   // "areaData.name": e.detail.value,
    //   "catetoryData.name": e.detail.value,
    //   commandType: e.detail.index + 1
    // })
    this.setData({
      isCatetory: false,
      // koulingList: arr,
      // "areaData.name": e.detail.value,
      "catetoryData.name": e.detail.value,
      commandType: e.detail.index + 1
    })

    // console.log(this.data.commandType, 'eeeeeee')
    // console.log(this.data.areaData)
  },
  cancelCatetory() {
    this.setData({
      isCatetory: false
    })
  },
  clickCatetory() {
    let temp = !this.data.isCatetory
    this.setData({
      isCatetory: temp
    })
  },
  // 添加时间
  addTime() {
    this.setData({
      addTimeShow: true
    })
  },
  // 添加星期
  addWeek() {
    this.setData({
      addWeekShow: true
    })
  },
  // 添加日期
  addDate() {
    this.setData({
      addDateShow: true
    })
  },
  cancelTime() {
    this.setData({
      isAddTime: false
    })
  },
  // 关闭添加日期弹窗
  onAddDayClose() {
    console.log('guanbi')
  },
  onChangeTime(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    // console.log(picker.getColumnValue(0))
    if (index == 0) {
      let myDays = daysMethod(getdays(picker.getIndexes()[0] + 1))
      picker.setColumnValues(1, myDays.values);
    }
    //  console.log(this.data.columnsTime)
  },
  confirmTime(e) {
    if (!this.data.ad_img_id) {
      Notify({
        type: 'primary',
        message: '请选择广告'
      });
      return false
    }

    this.setData({
      isAddTime: false
    })
    let value = e.detail.value
    let time = `${value[0]}-${value[1]} ${value[2]}:00-${value[3]}:00`
    let json = {}
    // debugger
    json.gid = this.data.ggz.id;
    json.adId = this.data.ad_img_id;
    json.adName = this.data.ggz.adName;
    json.time = time;

    let monthValue = value[0].substring(0, value[0].length - 1);
    let dayValue = value[1].substring(0, value[0].length - 1);
    json.value = `${monthValue}-${dayValue} ${value[2]}:${value[3]}`;

    let now = this.data.adTimeList
    now.push(json)
    // console.log(now)
    this.setData({
      adTimeList: now
    })
    // console.log(this.data.adTimeList)
  },
  delTime(e) {
    let index = e.currentTarget.dataset.index;
    let a = this.data.adTimeList;
    a.splice(index, 1);
    this.setData({
      adTimeList: a
    })
  },
  // 点击未选中
  clickUncheck(event) {
    // console.log(event, 'clickUncheck')
    let index = event.target.dataset.index
    // console.log(this.data.actionsAd[index].checked, index, 'this.data.actionsAd')
    this.data.actionsAd[index].checked = !this.data.actionsAd[index].checked
    // console.log(this.data.actionsAd[index].checked, 'checked')
    this.setData({
      actionsAd: this.data.actionsAd
    })
  },
  // 点击未选中--分类
  clickUncheckCate(event) {
    // console.log(event, 'clickUncheckCate')
    let index = event.target.dataset.index
    // console.log(this.data.catetoryColumns[index].checked, index, 'this.data.catetoryColumns')
    this.data.catetoryColumns[index].checked = !this.data.catetoryColumns[index].checked
    // console.log(this.data.catetoryColumns[index].checked, 'checked')
    this.setData({
      catetoryColumns: this.data.catetoryColumns
    })
  },
  onLoad: function (options) {
    console.log(options,'options')
    // if(options.info){
    //   let info = JSON.parse(options.info)
    //   this.setData({
    //     ad_img: info.ad_img
    //   })
    // }
    if (options.selected) {
      this.data.liuliangList = JSON.parse(options.selected)
      this.setData({
        liuliangList: this.data.liuliangList,
        isPageType: options.isPageType
      })
    }

    // 获取广告主
    this.adverUsersList()
    // 获取区域
    this.getWxAreaList()
    // 获取默认中专页面
    this.getAdLeadTo();
  },
  saoma() {
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        console.log(res, '扫码')
        //   // let url = res.result.toString();
        //   console.log(res.result)
        //   let scene = res.path.match("[\?\&]scene=([^\&]*)(\&?)", "i")[1]
        //   // res.path.matchReg = /(?<=?)/
        //   let path = res.path.split('?')[0]
        // console.log(scene,'scene',path)
        //   if (scene) {
        //       // wx.navigateTo({
        //       //     url: "/pages/nbsm/nbsm?sn=" + sn
        //       // })
        //       let url1 = "/" + path
        //       console.log(url1,'url1')
        //       wx.navigateTo({
        //         url: url1 + '?scene=' + scene
        //     })
        //   }
        // wx.setStorageSync('twcode_url', url)

      },
      fail() {
        wx.showToast({
          title: '扫码失败',
          icon: 'error',
          duration: 2000
        })
      }
    })
  },
  //获取广告主
  adverUsersList() {
    let _this = this;
    App._get('User/adverUsersList', {

    }, res => {
      res.data.map(item => {
        item.name = item.nickName
        item.checked = false
        return item;
      })

      _this.setData({
        actionsAd: res.data
      })
      console.log(_this.data.actionsAd, 'actionsAd')
    }, false, () => {

    })
  },
  //获取广告主的广告
  getAdverList(id) {
    console.log(id, 'idididididdi')
    let _this = this;
    App._get('Advertisement/getAdUserAdList', {
      userId: id
    }, res => {
      _this.setData({
        actionsAdList: res.data.list,
        showAd: false,
        showAdList: true
      })

    }, false, () => {})
  },
  clickCheck(e) {
    // console.log(e, 'clickCheckclickCheck')
  },
  // 选择广告图片
  selectAdImg(e) {
    let _this = this;
    let item = e.currentTarget.dataset.item;
    // debugger
    _this.setData({
      ad_img_id: item.id,
      'ggz.adName': item.f_adver_name
    })
  },
  batchChoice() {
    wx.navigateTo({
      url: '/pages/salesman/batchChoice/batchChoice?areaId=' + this.data.areaData.id
    })
  },
  onShow() {
    let arr = wx.getStorageSync('liuList')
    this.setData({
      liuliangList1: arr,
      koulingList1: arr
    })
    // console.log(this.data.koulingList)
  },
  delLiuliang(e) {
    // console.log(e.currentTarget.dataset.index,"delLiuliang")
    let index = e.currentTarget.dataset.index
    this.data.liuliangList.splice(index, 1)
    this.setData({
      liuliangList: this.data.liuliangList
    })
  },
  pageType() {
    let e = this;
    this.setData({
      isPageType: e.data.isPageType == 1 ? 2 : 1,
      liuliangList: []
    })
    // console.log(e.data.isPageType)
    if (e.data.isPageType == 2) {
      wx.navigateTo({
        url: '/pages/liuliangchiList/liuliangchiList?type=1' + '&isPageType=' + this.data.isPageType,
      })
    }
  },
  // switch外部跳转切换
  switch1Change(e) {
    this.setData({
      switch1Checked: e.detail.value
    })
  },

  switch2Change(e) {
    this.setData({
      switch2Checked: e.detail.value
    })
  },

  radioChange(e) {
    // console.log(e);
    this.setData({
      radioChecked: e.detail.value
    })
  },
  tipsVideo() {
    this.setData({
      isTipsVideoShow: true
    })
  },
  // 关闭提示
  onTipsClose() {
    this.setData({
      isTipsShow: false,
      isTipsVideoShow: false
    })
  },

  //获取区域
  getWxAreaList(page = 1) {
    let _this = this;
    App._get('Area/WxAreaList', {
      page,
      userId: wx.getStorageSync('user_id')
    }, res => {
      let list = [];
      res.data.map(item => {
        list.push(item.f_area_name)
      })

      _this.setData({
        wxAreaList: res.data,
        areaColumns: list
        // areaColumns: [
        //       "张三", "张一三", "张三丰", "李四", "哈哈哈"
        //     ]
      })
    }, false, () => {

    })
  },
  // 提交赋能
  submit(e) {
    // console.log(e)
    let _this = this,
      value = e.detail.value;
    // 表单校验
    if (!_this._onVerify(value)) {
      return false
    }
    // console.log(value, 'value', _this.data.isPageType, _this.data.koulingList)

    if (_this.data.isPageType == '1') {
      value.option = JSON.stringify(_this.data.adTimeList)
      value.commandType = _this.data.commandType
    } else {
      value.adverUserId = this.data.ggz.id;

    }
    // console.log(_this.data.adTimeList)

    // if (_this.data.radioChecked == 'r2') {
    //   value.commandVal = value.commandVal
    // }



    value.emptyQrcodeIds = _this.data.batchId
    value.type = _this.data.isPageType
    value.fnType = _this.data.radioChecked == 'r1' ? '1' : '2'
    if (_this.data.radioChecked == 'r1') {
      // console.log('r11111111',this.data.selectTimeList,this.data.chooseIndex,this.data.liuliangList)
      let opp = {
        uid: wx.getStorageSync('user_id'),
        selectTimeList: this.data.selectTimeList,
        liuliangList: this.data.liuliangList,
        timeType: this.data.chooseIndex == 0 ? 'day' : this.data.chooseIndex == 1 ? 'week' : this.data.chooseIndex == 2 ? 'month' : 'lunxun',
        ad_img: this.data.ad_img
      }
      value.option = JSON.stringify(opp)
      // value.option = JSON.stringify(this.data.selectTimeList)
      // value.liuliangList = JSON.stringify(this.data.liuliangList)
      // value.timeType = this.data.chooseIndex == 0 ? 'day' : this.data.chooseIndex == 1 ? 'week' : this.data.chooseIndex == 2 ? 'month' : 'lunxun'
    }
    if (_this.data.radioChecked == 'r2') {
      let commandTotal = ''
      let commandTypeTotal = []
      _this.data.koulingList.forEach(item => {
        // console.log(item.val, 'valllll')
        if (item.type == 1) {
          commandTotal = item.val + commandTotal
        } else {
          commandTotal = commandTotal + item.val
        }

        commandTypeTotal.push(item.type)
      })
      // console.log(value, 'value', commandTotal)
      let params = {
        commandType: JSON.stringify(commandTypeTotal),
        emptyQrcodeIds: value.emptyQrcodeIds,
        fnType: value.fnType,
        option: value.option,
        type: value.type,
        commandVal: commandTotal.replace(/抖音/g, '').replace(/淘宝/g, '')
      }
      // console.log(params, 'paramsparams')
      value = params
      value.ad_img = this.data.ad_img;
    }

    // console.log(value, 'valuevaluevaluevalue')

    // 提交表单
    _this.setData({
      reLoading: true,
    })
    App._post_form('Advertisement/batchEnabling', value, res => {
      if (res.code == 1) {
        App.showSuccess("操作成功!", () => {
          wx.redirectTo({
            url: '/pages/salesman/mine/mine'
          })
        })
      }
    }, false, () => {
      _this.setData({
        reLoading: false
      })
    })

  },


  // 表单验证
  _onVerify(data) {
    let _this = this;

    // if (!data.commandVal && _this.data.radioChecked == 'r2') {
    //   Notify({
    //     type: 'primary',
    //     message: '请填写口令'
    //   });
    //   return false
    // }
    // console.log(this.data.koulingList, _this.data.koulingList.some(item => item.val === ''), _this.data.radioChecked, 'hhhaahah')
    if (_this.data.koulingList.some(item => item.val === '') && _this.data.radioChecked == 'r2') {

      Notify({
        type: 'primary',
        message: '请填写口令'
      });
      return false
    }


    // if (!_this.data.ggz.id && _this.data.radioChecked == 'r1') {
    //   Notify({
    //     type: 'primary',
    //     message: '请选择广告主'
    //   });
    //   return false
    // }


    if (_this.data.isPageType == '1' && _this.data.radioChecked == 'r1') {
      // if (!_this.data.adTimeList.length) {
      //   Notify({
      //     type: 'primary',
      //     message: '请选择投放时间'
      //   });
      //   return false
      // }
      if (!_this.data.selectTimeList.length) {
        Notify({
          type: 'primary',
          message: '请选择投放时间'
        });
        return false
      }
    }

    if (!_this.data.batchId.length) {
      Notify({
        type: 'primary',
        message: '请选择要设置的码'
      });
      return false
    }

    return true;
  }
});